package deep10jvmdvm;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/*
    深入理解 AQS 和 CAS 原理

    自定义 AQS
 */
public class MyLock {
    private Sync sync = new Sync();

    /**
     * 加锁
     */
    public void lock() {
        sync.acquire(1);
    }


    public void unlock() {
        sync.release(1);
    }

    static class Sync extends AbstractQueuedSynchronizer {
        @Override
        protected boolean tryAcquire(int arg) {
            return compareAndSetState(0, 1);
        }

        @Override
        protected boolean tryRelease(int arg) {
            setState(0);
            return true;
        }
    }
}
